﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RemoteBank.Entities;
using RemoteBank.BLL;
using System.Data.SqlClient;
namespace RemoteBank
{
    public partial class frmMain : Form
    {
        private bool isLogin = false;
        private RemoteBankBLL remotebll = new RemoteBankBLL();
        private User user;
        private List<Account> listAccount;
        private SqlDataReader dr;
        internal List<Account> ListAccount
        {
            get { return listAccount; }
            set { listAccount = value; }
        }
        internal User User
        {
            get { return user; }
            set { user = value; }
        }
        private Account account;

        internal Account Account
        {
            get { return account; }
            set { account = value; }
        }

        public bool IsLogin
        {
            get { return isLogin; }
            set { isLogin = value; }
        }
        public frmMain()
        {
            InitializeComponent();
        }
        public void CreateLoginForm()
        {
            if (!isLogin)
            {
                frmLogin login = new frmLogin(this);
                login.MdiParent = this;
                login.StartPosition = FormStartPosition.CenterScreen;
                login.Show();
            }

        }
        private void frmMain_Load(object sender, EventArgs e)
        {

            //this.CreateLoginForm();
                   
        }

        private void changePaswordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(Convert.ToString(user.Username));
        }

        //dung de dong het cac form 
        public void CloseForm()
        {
            int child = MdiChildren.Length;
            for (int i = 0; i < child; i++)
            {
                MdiChildren[i].Dispose();
            }
            
        }
        //public void Enabledbutton()
        //{
        //    transferToolStripMenuItem.Enabled = false;
        //    transferToolStripMenuItem1.Enabled = false;
        //    receivingToolStripMenuItem.Enabled = false;
        //    receivingToolStripMenuItem1.Enabled = false;
        //    withdrawlToolStripMenuItem.Enabled = false;
        //    withdrawlToolStripMenuItem1.Enabled = false;
        //}

        private void logOutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseForm();
            isLogin = false;
            this.CreateLoginForm();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
           // Enabledbutton();
            this.CreateLoginForm();
            frmMain_Load(sender, e);
            if (user != null)
            {
                listAccount = new List<Account>();
                dr = remotebll.getAccountByUserID(user.Userid);

                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        account = new Account();
                        account.Accountid = dr.GetInt32(0);
                        account.Userid = dr.GetInt32(1);
                        account.Accountname = dr.GetString(2);
                        account.Type = dr.GetInt32(3);
                        account.Balance = Convert.ToDouble(dr.GetSqlMoney(4).ToString());
                        
                        //if (account.Type == 1)
                        //{
                        //    receivingToolStripMenuItem.Enabled = true;
                        //    receivingToolStripMenuItem1.Enabled = true;
                        //}
                        //if (account.Type == 2)
                        //{
                        //    transferToolStripMenuItem.Enabled = true;
                        //    transferToolStripMenuItem1.Enabled = true;

                        //}
                        //if (account.Type == 3)
                        //{
                        //    receivingToolStripMenuItem.Enabled = true;
                        //    receivingToolStripMenuItem1.Enabled = true;
                        //}
                        listAccount.Add(account);
                    }
                }
                //dr.Close();
            }
            else
            {
                MessageBox.Show("User is Null !!!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            //CloseForm();
            //frmAccountInformation accinfo = new frmAccountInformation();
            //accinfo.MdiParent = this;
            //accinfo.WindowState = FormWindowState.Maximized;
            //accinfo.Show();
        }

    }
}
